<template>
  <Dialog
    v-model="show"
    @confirm="handleSubmit"
    @close="close"
    :title="$t('sellerManage.setRateDia.设置分润比例')"
    width="400px"
  >
    <el-form :model="formData" label-width="120px" ref="formRef" :rules="rules">
      <el-form-item :label="$t('sellerManage.setRateDia.分润比例')" prop="commissionRate">
        <el-input-number
          v-model="formData.commissionRate"
          :min="0"
          :max="100"
        />
      </el-form-item>
    </el-form>
  </Dialog>
</template>
<script setup>
import { ref, watch } from "vue";
import Dialog from "@/components/dialog.vue";
import Api from "@/config/api/api";
import { ElMessage } from "element-plus";
import { useI18n } from "vue-i18n";

const { t: $t } = useI18n();

const show = defineModel();
const emit = defineEmits(["success"]);
const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
});
watch(show, (val) => {
  if (val) {
    props.data && (formData.value.commissionRate = props.data.commissionRate);
  }
});
const formData = ref({
  commissionRate: "",
});
const rules = ref({
  commissionRate: [
    { required: true, message: $t('sellerManage.setRateDia.请输入分润比例'), trigger: "blur" },
  ],
});
const formRef = ref();
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      Api.updateCommissionRate({
        id: props.data.id,
        commissionRate: formData.value.commissionRate,
      }).then((res) => {
        if (res?.status == 200) {
          ElMessage.success($t('sellerManage.setRateDia.保存成功'));
          close();
          emit("success");
        }
      });
    }
  });
};
const close = () => {
  formData.value.commissionRate = "";
  show.value = false;
};
</script>
